
<template>
  <div>
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="题目分类名称：">
          <el-input v-model="formInline.userDo" placeholder="请输入"></el-input>
        </el-form-item>
         <el-select v-model="value" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
        <el-form-item>
          <el-button type="primary" @click="findfpool()">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">重置</el-button>
          <!-- <el-button type="primary" @click="sonfind()">666</el-button> -->
        </el-form-item>
      </el-form>
      <el-row>
        <el-button type="primary" @click="sub">新建题库分类</el-button>

        <el-dialog
          title="新增"
          :visible.sync="visible"
          width="30%"
          :before-close="handleClose"
        >
                 
          <div>
            <el-form
              :inline="true"
              :model="formInline"
              class="demo-form-inline"
            >
              <el-form-item label="题库名称">
                <el-input
                  v-model="formInline.poolName"
                  placeholder="题库名称"
                  id="n"
                ></el-input>
              </el-form-item>
              <!-- <el-form-item label="创建人员">
                <el-input
                  v-model="formInline.createMan"
                  placeholder="创建人员"
                ></el-input>
              </el-form-item> -->

               <el-form-item label="父库名称">
                <el-input
                    :disabled="disabled"
                  v-model="formInline.poolName2"
                  placeholder="父库名称"
                ></el-input>
              </el-form-item>
            </el-form>

                       
          </div>
                     
          <span slot="footer" class="dialog-footer">
                         
            <el-button @click="visible = false">取 消</el-button>              
            <el-button :plain="true" type="primary" @click="insert()">确 定</el-button>      
                 
          </span>
                   
        </el-dialog>

        <!-- <el-button type="success">批量删除</el-button> -->
      </el-row>
    </div>

    <!-- <MySearch></MySearch> -->
    <el-table
      ref="multipleTable"
      :data="subject"
      tooltip-effect="dark"
      style="width: 100%; text-align: center; margin-top: 10px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" prop="id" label="序号" width="50">
      </el-table-column>
      <!-- <el-table-column fixed prop="id" label="id" width="70"> </el-table-column> -->
      <el-table-column fixed prop="poolName" label="题库分类名称" width="150">
      </el-table-column>
      <!-- <el-table-column prop="fid" label="分类层级" width="120">
      </el-table-column> -->
      <el-table-column prop="order" label="排序值" width="120">
      </el-table-column>
      <el-table-column prop="createMan" label="创建人" width="120">
      </el-table-column>
      <el-table-column prop="updateTime" label="最新修改时间" width="165">
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <router-link to="/issue/categoryson">
<el-button @click="findAllson(scope.row)"

type="text" size="small"
            >查看</el-button
          >

          </router-link>
          
          <el-button @click="handleClick(scope.row)" type="text" size="small"
            >编辑</el-button
          >

           <el-dialog
            :append-to-body="true"
          title="编辑"
          :visible.sync="visibles"
          width="30%"
          :before-close="handleClose"
        >
                 
          <div>

            <el-form
              :inline="true"
              :model="formInline"
              class="demo-form-inline"
            >
              <el-form-item label="题库名称">
                <el-input
                  v-model="formInline.poolName"
                  placeholder="题库名称"

                ></el-input>
              </el-form-item>
            </el-form>

                       
          </div>
                     
          <span slot="footer" class="dialog-footer">
                         
            <el-button @click="visibles = false">取 消</el-button>              
            <el-button type="primary" @click="update()">确 定</el-button>      
                 
          </span>
                   
        </el-dialog>






          <el-button slot="reference" type="text" size="small" @click="delete01(scope.row)" >删除</el-button>

          <!-- <el-button type="text" @click="dialogVisible = true">点击打开 Dialog</el-button> -->

<el-dialog
:append-to-body="true"
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <span>确认删除?</span>
  <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="delete02()">确 定</el-button>
  </span>
</el-dialog>

          


 <!-- <el-button :plain="true" @click="open">打开消息提示</el-button> -->
         
        </template>
      </el-table-column>
    </el-table>
    
      <div>
          <div class="block">
    <span class="demonstration"></span>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[2, 5, 8, 10]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
      </div>
    
   
  </div>
  
</template>

<script>
import MySearch from "@/views/issue/search.vue";
import { postData } from "@/utils/Util";



export default {
  created() {
    // this.findall();
   this.findallall();

  //  if (this.value=="") {
  //    console.log("666");
  //    this.disabled=false
  //  }
  },
  methods: {

 


    findallall(){

      this.$axios.post(
         "http://localhost:8080/pool/findAllAll/"+this.page+"/"+this.limit
      ).then(res=>{
        console.log(res);
        this.total=res.data.data.total
        // this.subject=res.data.data
        if (res.data.code===500) {
          this.$message(res.data.data)
          
        }else{
          this.total=res.data.data.total
          
          console.log(res);

          this.subject=res.data.data.list
        }

      })

    },

    // aaa(){console.log(this.value);},
    sonfind(){
      // console.log(this.formInline.userDo);
     
      this.formInline.userDo

    console.log(this.value);
      

    },

    findallsons(){
      this.$axios(
         "http://localhost:8080/pool/findspools"

      ).then(res=>{
        console.log(res.data.data);
         this.subject=res.data.data

      })
    },





    findfpool(){
      console.log(this.formInline.userDo);
      console.log(this.value);
      

       if (this.value.
      length===0 && this.formInline.userDo=="" ) {
        this.$message("请输入关键字")
      }else if(this.value.length===0){

      // console.log(this.formInline.userDo);
        // this.$message("请输入题库关键字")
          this.$axios.post(
    "http://localhost:8080/pool/findAllAllByName/",{
      poolName:this.formInline.userDo
    }

  ).then(res=>{
    if (res.data.code==500) {
      this.$message(res.data.data)
    }else{
       this.subject=res.data.data

    }
    // console.log(res);
   
  }) }else if (this.formInline.userDo==""&&this.value==="父题库") {

    console.log("展示所有父题库");
     this.findall();
 }else if(this.formInline.userDo==""&&this.value==="子题库"){
   console.log("展示所有所有子题库");
   this.findallsons();

 }else if(this.value==="父题库"&&this.formInline.userDo!=""){
  //  console.log("根据父库关键字模糊查询展示父题库-------------根据String和fid为空来找");
      // console.log(this.formInline.userDo);
      
       this.$axios.post(
    "http://localhost:8080/pool/findByName/"+this.formInline.userDo,

  ).then(res=>{
    if (res.data.code==500) {
      this.$message(res.data.data),
       this.findall();



    }else{
      this.subject=res.data.data
    }
    // console.log(res);
    
  })
  

 }else if (this.value==="子题库"&&this.formInline.userDo!="") {
   console.log("根据子库关键字模糊查询展示子题库-------------根据String找");
  //  console.log(this.formInline.userDo);
   this.formInline.poolName=this.formInline.userDo
   this.$axios.post(
    
 "http://localhost:8080/pool/findSonPoolName",this.formInline
   ).then(res=>{
     if (res.data.code==500) {
       this.$message(res.data.data)
       this.findallsons();
     }
     console.log(res);
     this.subject=res.data.data

   })


 }
 
 else{
   console.log("两条件查询");
 }



    

    

      
      // console.log(this.formInline.userDo);
       
 

    },

    findfpools(){
  this.$axios.post(
    "http://localhost:8080/pool/findByName/"+this.formInline.userDo,

  ).then(res=>{
    console.log(res);
    this.subject=res.data.data
  })
    },


    update01(){
      this.visibles=true;

    },


    delete02() {
      
      // console.log(this.formInline.id);
      this.$axios.post(
        "http://localhost:8080/pool/delete/"+this.formInline.id,{}
         
      ).then(res=>{
        // this.findall(),
        // this.findallall();
   if (res.data.code===500 ) {
     this.findall(),
     this.$message(res.data.data)
       }else if (res.data.code===520) {
         this.findall(),

     this.$message(res.data.data)
         
       }else if (res.data.code===200) {
         
       
         
          console.log(res),

         
          this.$message("成功删除"+res.data.data+"条数据"),
          this.dialogVisible = false;
          this.findallall()
       }
 // this.$message('删除成功'),
        this.dialogVisible = false;
        

      })
    

      this.visibles = false;
    },

    delete01(row) {
      // console.log(666);
      this.formInline.id=row.id,

      // console.log(row);
      this.dialogVisible = true;
    },



add01(){

       this.$axios.post("http://localhost:8080/pool/insertSpool",this.formInline, {
        // poolName:this.poolName2
      }).then(res=>{


        console.log(res.data);
        if ( res.data.code===200) {
          this.$message('添加成功');
        }else{
          this.$message(res.data.data+',添加失败');
        }   

         this.$axios.post("http://localhost:8080/pool/findFidPool",{

            fid:this.fids.fid
          }
          
          ).then(res=>{
            console.log(res);
            this.subject=res.data.data

          })
          console.log(this.fids.fid);
        // this.findfdelete()
      });
      
      this.visible = false;



    },



    insert() {
      console.log(this.formInline.poolName2);
// 添加父库
      if (this.formInline.poolName2=="") {

        this.$axios.post("http://localhost:8080/pool/insertFpool",this.formInline,).then(res=>{

        // console.log(res.data.code);
        // console.log(this.MyPage.handleCurrentChange);
        if ( res.data.code===200) {
          this.$message('添加成功');
        }else{
          this.$message('添加失败,当前已有此题库!');
        }


        this.findall();
      });
      
      this.visible = false;
      }else if (this.formInline.poolName2!="") {
        //this.formInline.poolName2为fid,根据此fid查到父题库id,查到此父id对应的名称填入父库中
        this.$axios.post(
   "http://localhost:8080/pool/findidPool/"+this.formInline.poolName2
      ).then(res=>{
        // console.log(666);
        if (res.data.code===500) {
          this.$message(res.data.data)
        }
        // console.log(res.data.data);
        this.fids.fid=res.data.data,
       this.formInline.fid=res.data.data,
        
          this.add01();
        }
  
        
      )

      }
 
      
    },

    sub() {
      console.log(this.value);
     if ( this.value==="父题库") {
       this.disabled=true
      }else if (this.value==="子题库" ) {
         this.disabled=false 
      }else if (this.value=="") {
        this.disabled=false
      }
       this.visible = true;
    },

    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
   

    update() {
      // console.log(666);
      // console.log(this.formInline.id),
       
      this.$axios.post("http://localhost:8080/pool/update",{
        id:this.formInline.id,
        poolName:this.formInline.poolName
      })
        .then((res) => {
         if (this.formInline.userDo=="") {
           this.$message("请输入修改名称!")
         }else if (this.value==="父题库") {
           this.findall()
         }else if (this.value==="子题库") {
           this.findallsons()
         }else{
           findallall()

         }
          
          this.$message('编辑成功'),
           this.findall();
        })
      this.visibles=false;

      // console.log(999);
    },

    handleSelectionChange() {},

    onSubmit() {
      // console.log("submit!");
      this.formInline.userDo="",
      this.value="",
      this.findallall();
      // this.findall()
    },


    findAllson(row){
      this.formInline.id=row.id,
      this.formInline.poolName=row.poolName

      this.$router.push({
       "path":"/issue/categoryson",
       "query":{
        "id":this.formInline.id,
        "poolName":this.formInline.poolName

    }
   
}) },

    findall() {
      this.$axios
        .post(
          "http://localhost:8080/pool/findAll/" +
            this.page +
            "/" +
            this.limit
          
        )
        .then((res) => {
          console.log(res.data.data);
          this.subject = res.data.data.list;
        })
        
    },

    handleClick(row) {

      this.visibles=true,
      console.log(row);
      this.formInline.id=row.id;
      // console.log(this.id),
      this.formInline.poolName=row.poolName;
      // console.log(this.formInline.poolName);
      
    },
    
    handleSizeChange(val) {
      this.limit=val
        console.log(`每页 ${val} 条`);
        console.log(this.limit);
        // this.findall()
       this.findallall()

      },
      handleCurrentChange(val) {
        this.page=val
        console.log(`当前页: ${val}`);
        console.log(this.page);
        // this.findall()
        this.findallall()
      }




  },
  components: {
    MySearch,
    // Page,
    
  },

  data() {
    return {
      
      disabled:false,

      centerDialogVisible: false,
      dialogVisible:false,
      visible: false,
      visibles: false,

      form: {},

      formInline: {
        id:"",
        userDo: "",
        region: "",
        poolName: "",
        createMan: "超级管理员",
        updateMan:"",
        fid: "",
        poolName2:""
      },

      forms: {
        poolName: "",
      },

      subject: [],

      pageinfo: {
        page: 1,
        limit: 10,
      },

      su:{
        poolName:"",
      },

     options: [{
          value: "父题库",
          label: '父题库'
        },{
            value: "子题库",
          label: '子题库'
        }],
     value:[],

       fids:{
        fid:""
      },

    currentPage4: 1,
     total:100,
     limit:10,
     page:1


   




    };
  },
};
</script>